import {traverseTree} from "./tools";

export class TreeNode {
  constructor(data) {
    const { id } = data
    this.id = typeof id === 'undefined' ? new Date().valueOf() : id
    // other params
    this.groups = null
    // this.isLeaf=data.hasOwnProperty("tags")//最后的节点
    for (var k in data) {
      if (k !== 'groups') {
        this[k] = data[k]
      }
    }
  }
  changeName(name) {
    this.name = name
  }
  addChildren(children) {
    if (!this.groups) {
      this.groups = []
    }
    if (Array.isArray(children)) {
      for (let i = 0, len = children.length; i < len; i++) {
        const child = children[i]
        child.parent = this
      }
      this.groups.concat(children)
    } else {
      children.parent=this;
      this.groups.push(children)
    }
  }
  // remove self
  remove() {
    const parent = this.parent
    const index = parent.findChildIndex(this)
    parent.groups.splice(index, 1)
  }


  // remove child
  _removeChild(child) {
    for (var i = 0, len = this.groups.length; i < len; i++) {
      if (this.groups[i] === child) {
        this.groups.splice(i, 1)
        break
      }
    }
  }

  isTargetChild(target) {
    let parent = target.parent
    while (parent) {
      if (parent === this) {
        return true
      }
      parent = parent.parent
    }
    return false
  }

  moveInto(target) {
    if (this.name === 'root' || this === target) {
      return
    }

    // cannot move ancestor to child
    if (this.isTargetChild(target)) {
      return
    }

    // cannot move to leaf node
    if (target.isLeaf) {
      return
    }

    this.parent._removeChild(this)
    this.parent = target
    this.pid = target.id
    if (!target.groups) {
      target.groups = []
    }
    target.groups.unshift(this)
  }

  findChildIndex(child) {
    var index
    for (let i = 0, len = this.groups.length; i < len; i++) {
      if (this.groups[i] === child) {
        index = i
        break
      }
    }
    return index
  }

  _canInsert(target) {
    if (this.name === 'root' || this === target) {
      return false
    }

    // cannot insert ancestor to child
    if (this.isTargetChild(target)) {
      return false
    }

    this.parent._removeChild(this)
    this.parent = target.parent
    return true
  }

  insertBefore(target) {
    if (!this._canInsert(target)) return

    const pos = target.parent.findChildIndex(target)
    target.parent.groups.splice(pos, 0, this)
  }

  insertAfter(target) {
    if (!this._canInsert(target)) return

    const pos = target.parent.findChildIndex(target)
    target.parent.groups.splice(pos + 1, 0, this)
  }

  toString() {
    return JSON.stringify(traverseTree(this))
  }
}

export class Tree {
  constructor(data) {
    this.root = new TreeNode({name: 'root'})
    this.initNode(this.root, data)
    // console.log(this.root)
    return this.root
  }

  initNode(node, data) {
    for (let i = 0, len = data.length; i < len; i++) {
      var _data = data[i]
      var child = new TreeNode(_data)
      if (_data.groups && _data.groups.length > 0) {
       this.initNode(child, _data.groups)
      }
      node.addChildren(child)
    }
  }
}
